package com.liyuxiang.exception.handler;

import com.liyuxiang.exception.SystemException;
import com.liyuxiang.vm.ResponseResult;
import com.liyuxiang.vm.enums.ResultStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
//全局异常捕获处理器
public class GlobalExceptionHandler {


    @ExceptionHandler(SystemException.class)
    public ResponseResult systemExceptionHandler(SystemException e) {
        //打印异常信息
//        log.error("出现异常{}", e);
        //从异常对象中获取异常信息，封装并返回
        return ResponseResult.errorResult(e.getCode(), e.getMsg());
    }


//    @ExceptionHandler(Exception.class)
//    public ResponseResult exceptionHandler(Exception e) {
//        log.error("出现了异常{}", e);
//        return ResponseResult.errorResult(ResultStatusEnum.SYSTEM_ERROR.getCode(), ResultStatusEnum.SYSTEM_ERROR.getMsg());
//    }

    @ExceptionHandler(BadCredentialsException.class)
    public ResponseResult badCredentialsExceptionHandler(BadCredentialsException e) {
//        log.error("用户名密码校验异常{}", e);
        return ResponseResult.errorResult(401, "密码错误");
    }

    //空指针异常
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseResult illegalArgumentException(IllegalArgumentException e) {
        return ResponseResult.errorResult(401, e.getMessage());
    }

}
